<template>
	<view class="view">
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index" @click="selectAddress(item)">
				<view class="user">{{item.name}}<text>{{item.phone}}</text></view>
				<view class="address">
					{{item.provinceName || ''}}{{item.cityName || ''}}{{item.areaName || ''}}{{item.address || ''}}
				</view>
				<view class="operate">
					<view class="default" @tap.stop="setDefault(item.id)">
						<image v-if="item.isDefault == 1" src="/static/images/smallselection.png"></image>
						<image v-else src="/static/images/smallsele.png"></image>
						<text>默认地址</text>
					</view>
					<view class="edit" @tap.stop="to('/pages/other/address/edit?id=' + item.id)">
						<image src="/static/images/editp.png"></image>
						<text>编辑</text>
					</view>
					<view class="delete" @tap.stop="deleteDialog(item.id)">
						<image src="/static/images/ic_delete.png"></image>
						<text>删除</text>
					</view>
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="btn" @tap.stop="to('/pages/other/address/edit')">添加收货地址</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 0,
				finished: false,
				pageNo: 1,
				list: []
			}
		},
		onLoad(options) {
			this.type = options.type || 0;
		},
		onShow() {
			this.init();
		},
		onReachBottom() {
			this.getList();
		},
		onPullDownRefresh() {
			this.init();
		},
		methods: {
			init() {
				this.finished = false;
				this.pageNo = 1;
				this.list = [];
				this.getList();
			},
			getList() {
				if (this.finished) {
					return;
				}
				this.$api.http('/user/userAddress/list', 'GET', {
					pageNo: this.pageNo++,
					pageSize: 10
				}, res => {
					if (res.data.code == 200) {
						if (res.data.result.records.length < 10) {
							this.finished = true;
						}
						this.list = this.list.concat(res.data.result.records);
					} else {
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
					uni.stopPullDownRefresh();
				})
			},
			to(url) {
				uni.navigateTo({
					url: url
				});
			},
			deleteDialog(id) {
				const that = this;
				uni.showModal({
					title: '删除地址',
					content: '您确定要删除该地址吗？',
					success(res) {
						if (res.confirm) {
							that.$api.http('/user/userAddress/delAddress', 'DELETE', {
								addressId: id
							}, res => {
								if (res.data.code == 200) {
									that.init();
								} else {
									that.$Toast(res.data.message || '删除失败，请重试！');
								}
							})
						}
					}
				});
			},
			setDefault(id) {
				this.$api.http('/user/userAddress/setDefaultAddress', 'GET', {
					addressId: id
				}, res => {
					if (res.data.code == 200) {
						this.init();
					} else {
						this.$Toast(res.data.message || '设置失败，请重试！');
					}
				})
			},
			selectAddress(item) {
				if (this.type == 1) {
					this.getOpenerEventChannel().emit('acceptDataEvent', item);
					uni.$emit('selectAddress', item);
					uni.navigateBack();
				}
			},
		}
	}
</script>

<style lang="less" scoped>
	.view {
		width: 100%;
		min-height: 100vh;
		background-color: #F1F1F1;

		.list {
			margin-bottom: 120rpx;

			.item {
				margin-bottom: 30rpx;
				background-color: #FFFFFF;

				.user {
					padding: 20rpx 30rpx;
					font-size: 28rpx;

					text {
						margin-left: 30rpx;
					}
				}

				.address {
					padding: 0 30rpx 30rpx 30rpx;
					font-size: 26rpx;
					color: #666666;
				}

				.operate {
					display: flex;
					flex-direction: row;
					align-items: center;
					padding: 20rpx 30rpx;
					border-top: 1rpx solid #F1F1F1;

					.default {
						display: flex;
						flex-direction: row;
						align-items: center;
						flex: 1;
						font-size: 24rpx;
						color: #999999;

						image {
							width: 30rpx;
							height: 30rpx;
							margin-right: 10rpx;
						}
					}

					.edit {
						display: flex;
						flex-direction: row;
						align-items: center;
						margin-right: 30rpx;
						font-size: 24rpx;
						color: #999999;

						image {
							width: 28rpx;
							height: 28rpx;
							margin-right: 10rpx;
						}
					}

					.delete {
						display: flex;
						flex-direction: row;
						align-items: center;
						font-size: 24rpx;
						color: #999999;

						image {
							width: 20rpx;
							height: 30rpx;
							margin-right: 10rpx;
						}
					}
				}
			}
		}

		.bottom {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;

			.btn {
				margin: 20rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				border-radius: 40rpx;
				font-size: 30rpx;
				color: #FFFFFF;
				background-color: #32C59A;
			}
		}
	}
</style>